#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QPushButton>
#include <QRadioButton>
#include <QMessageBox>
#include <QTextCodec>
#include <QFileDialog>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , qUtility(this)
{
    ui->setupUi(this);
    ui->tabWidget->setCurrentIndex(0);

    // ----------------------------------------------------------------------
    // F01：文件操作
    // 读文件最后一行
    connect(ui->f01Btn1, &QPushButton::clicked, this, &Widget::f01Slot1);
    connect(ui->f01Btn2, &QPushButton::clicked, this, &Widget::f01Slot2);
    connect(ui->f01Btn3, &QPushButton::clicked, this, &Widget::f01Slot3);
    connect(ui->f01Btn4, &QPushButton::clicked, this, &Widget::f01Slot4);
    // 获取文件编码 utf-8或gbk
    connect(ui->f01Btn5, &QPushButton::clicked, [=](){
        QString fileName = QFileDialog::getOpenFileName(this, "选择文件", "..\\resource\\f01", "*.txt");
        if (fileName.size() != 0) // 已选择文件
        {
            ui->f01TextBrowser->append(fileName); // 显示文件名
            ui->f01TextBrowser->append("只检查前5行："+qUtility.getFileCodec(fileName)); // 只检查前5行（默认值）
            ui->f01TextBrowser->append("全文件检查："+qUtility.getFileCodec(fileName, 0)); // 全文件检查
        }
    });

    // ----------------------------------------------------------------------
    // F02：计算CRC
    ui->f02Radio1->setChecked(true); // 选择Hex
    bF02IsHex = true;
    // 支持的CRC算法添加到下拉菜单中
    ui->f02ComboBox->addItem("CRC8");
    ui->f02ComboBox->addItem("CRC16/Modbus");
    ui->f02ComboBox->addItem("CRC16/XModem");
    ui->f02ComboBox->addItem("CRC32");
    // 使用匿名函数，获取输入的类型，Hex或Ascii
    connect(ui->f02Radio1, &QRadioButton::clicked, [=](){
        bF02IsHex = true;
    });
    connect(ui->f02Radio2, &QRadioButton::clicked, [=](){
        bF02IsHex = false;
    });
    connect(ui->f02ComboBox, &QComboBox::currentTextChanged, [=](QString str){
        qDebug() << str; // 选择的内容
    });
    // 点击进行计算
    connect(ui->f02BtnCalc, &QPushButton::clicked, this, &Widget::f02SlotCalc);

    // ----------------------------------------------------------------------
    // F03：DES加密算法
    connect(ui->f03Btn1, &QPushButton::clicked, this, [=](){
        QByteArray bytes = ui->f03TextEdit->toPlainText().toUtf8();
        qUtility.qDes.encrypt(bytes);
        ui->f03TextBrowser->setText(bytes.toHex());
    });
    connect(ui->f03Btn2, &QPushButton::clicked, this, [=](){
        QByteArray bytes = ui->f03TextEdit->toPlainText().toUtf8();
        bytes = QByteArray::fromHex(bytes);
        qUtility.qDes.decrypt(bytes);
        ui->f03TextBrowser->setText(QString(bytes));
    });
    connect(ui->f03Btn3, &QPushButton::clicked, this, [=](){
        QByteArray bytes = ui->f03TextEdit->toPlainText().toUtf8();
        qUtility.qDes.encrypt3(bytes); // 执行3DES加密算法
        ui->f03TextBrowser->setText(bytes.toHex());
    });
    connect(ui->f03Btn4, &QPushButton::clicked, this, [=](){
        QByteArray bytes = ui->f03TextEdit->toPlainText().toUtf8();
        bytes = QByteArray::fromHex(bytes);
        qUtility.qDes.decrypt3(bytes); // 执行3DES解密算法
        ui->f03TextBrowser->setText(QString(bytes));
    });

    // ----------------------------------------------------------------------
}

// ----------------------------------------------------------------------
//F01：文件操作
//读文件最后一行
void Widget::f01Slot1()
{
    QString fileName("../resource/f01/1.txt");
    f01Read(fileName);
}
void Widget::f01Slot2()
{
    QString fileName("../resource/f01/2.txt");
    f01Read(fileName);
}
void Widget::f01Slot3()
{
    QString fileName("../resource/f01/3.txt");
    f01Read(fileName);
}
void Widget::f01Slot4()
{
    QString fileName("../resource/f01/4.txt");
    f01Read(fileName);
}
void Widget::f01Read(QString &fileName)
{
    QFile f01File;
    f01File.setFileName(fileName);
    f01File.open(QIODevice::ReadOnly); // 只读方式打开

    // 读有内容的最后一行
    QByteArray f01Bytes = qUtility.readLastLine(f01File);
    if (f01Bytes.size() == 0)
    {
        QMessageBox::information(this, "提示", "文件为空");
    }
    else
    {
        // 文本框中追加显示
        ui->f01TextBrowser->append(QString(f01Bytes.data()));
    }

    f01File.close();
}

// ----------------------------------------------------------------------
//F02：计算CRC
void Widget::f02SlotCalc()
{
    QByteArray bytes = f02GetBytes(); // 获取文本输入框中的内容
    QByteArray crc;

    // 获取下拉菜单中当前选择的内容
    QString str = ui->f02ComboBox->currentText();
    if (str.compare("CRC8") == 0)
    {
        crc.append(qUtility.qCrc.calculateCRC8(bytes));
    }
    else if (str.compare("CRC16/Modbus") == 0)
    {
        uint16_t crc16 = qUtility.qCrc.calculateCRC16Modbus(bytes);
        crc.append(crc16 >> 8); // 追加内容
        crc.append(crc16 >> 0);
    }
    else if (str.compare("CRC16/XModem") == 0)
    {
        uint16_t crc16 = qUtility.qCrc.calculateCRC16XModem(bytes);
        crc.append(crc16 >> 8);
        crc.append(crc16 >> 0);
    }
    else // CRC32
    {
        uint32_t crc32 = qUtility.qCrc.calculateCRC32(bytes);
        crc.append(crc32 >> 24);
        crc.append(crc32 >> 16);
        crc.append(crc32 >> 8);
        crc.append(crc32 >> 0);
    }

    ui->f02LineEdit->setText(QString(crc.toHex()).toUpper()); // Hex大写字母显示
}
QByteArray Widget::f02GetBytes()
{
    QByteArray bytes = ui->f02TextEdit->toPlainText().toUtf8();

    // 如果是Hex输入，则做转换
    if(bF02IsHex)
    {
        bytes = QByteArray::fromHex(bytes);
    }

    return bytes;
}

// ----------------------------------------------------------------------
Widget::~Widget()
{
    delete ui;
}

